<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>02_人员案例</title>
  <!-- 引入Vue -->
  <script type="text/javascript" src="../js/vue.js"></script>
</head>

<body>
  <!-- 准备好一个容器-->
  <div id="demo">
    <h1>人员案例练习</h1>
    搜索:<input type="text" name="" id="" v-model="keyword"><br>
    <ul>
      <li v-for="(item,index) in persons2">{{item.name}}-{{item.age}}-{{item.gender}}</li>
    </ul>
  </div>
</body>

<script type="text/javascript">
  //阻止 vue 在启动时生成生产提示。
  Vue.config.productionTip = false;
  new Vue({
    el: '#demo',
    data: {
      keyword: '',
      persons: [
        { id: "wfsafsfgs01", name: '马冬梅', age: 18, gender: '女' },
        { id: "wfsafsfgs02", name: '周冬雨', age: 19, gender: '女' },
        { id: "wfsafsfgs03", name: '周杰伦', age: 20, gender: '男' },
        { id: "wfsafsfgs04", name: '文灶轮', age: 21, gender: '男' },
      ],
      persons2: [],
    },
    // methods: {
    //   keyword(e) {
    //     let result = this.persons.filter((item) => {
    //       return item.name.includes(e.target.value)
    //     })
    //     this.persons2 = result
    //     // console.log(result);
    //     // console.log(e.target.value);
    //   }
    // },
    watch: {
      keyword: {
        immediate: true,
        handler(value) {
          let result = this.persons.filter((item) => {
            return item.name.includes(value)
          })
          this.persons2 = result
          // console.log(result);
          // console.log(e.target.value);
        }
      }
    }

  })
</script>

</html>